const fs = require('fs');
const chalk = require('chalk');

// const get_meta  = require( "./oracle_get_meta");
const genTableColumnsMeta = require("./async_oracle_get_meta");
//测试 const r = get_meta('test_wxhpck_oss_analysis_prod').then(console.info).catch(console.error)
module.exports = function (plop) {
  plop.setGenerator('test2', {
    description: 'this is a skeleton plopfile',
    prompts: [
      {
        type: 'input',
        name: 'name',
        message: '请输入你的名字(忽略请按回车)'
      },
      {
        type: 'input',
        name: 'tablename',
        message: '请输入表名',
        validate: v => v.length > 0 || '必须*'
      },
      {
        when: answers => answers.tablename === 'table',
        type: 'confirm',
        name: 'tablename_confirm',
        message: '确认 使用保留字命名的表吗,这可能导致失败'
      },
      {
        when: function (answers) {
          if (answers.tablename_confirm===false) {
            console.log('退出');
            process.exit(0)
          }
          return true;
        },
        name: 'cols',
        message: '选择表的列,<空格>单选,<a>全选,<i>反选',
        type: 'checkbox',
        choices: async function (answers) {
          console.log('table name your select is', answers.tablename)
          let meta = await genTableColumnsMeta(answers.tablename);
          // console.log('meta get:::::', meta);
          if (meta.length === 0) {
            console.error('表不存在')
            process.exit(0)
          }
          return meta.map(s => ({
            name: `${(s['COLUMN_NAME']+'            ').slice(0,18)}--- ${s['DATA_TYPE']}     --- ${s['DATA_LENGTH']}`,
            value: s['COLUMN_NAME'],
            checked: true
          }))
        },
        validate: value => value.length > 0 || '至少1列'
      },
      {
        name: 'columns',
        description: '固定选择',
        message: '这是一个已知选项的问题',
        type: 'checkbox',
        choices: [
          {name: 'Cheese', value: 'cheese', checked: true},
          {name: 'Pepperoni', value: 'pepperoni'},
          {name: 'Pineapple', value: 'pineapple'},
          {name: 'Mushroom', value: 'mushroom'},
          {name: 'Bacon', value: 'bacon', checked: true}
        ]
      }
    ], // array of inquirer prompts
    actions: [
      function (answers) {
        console.log(chalk.blue('你好'), chalk.red(`${answers.name||'*'}`));
        console.log(`从表:${answers.tablename} ,你选择了:`, answers.cols);
      }
    ]  // array of actions
  });
};